<?php

namespace App\Listeners;

use App\Events\UserLearnVideoChangeEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class UserLearnVideoChangeListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param UserLearnVideoChangeEvent $event
     * @return void
     */
    public function handle(UserLearnVideoChangeEvent $event)
    {
        $userId = $event->userId;
        $learnVideoIds = DB::table('user_learn_video_logs')->where('userId', $userId)->pluck('learnVideoId')->toArray();
        $allSectionCount = DB::table('learn_videos')->whereIn('id', $learnVideoIds)->sum('section_count');
        DB::table('user')->where('id', $userId)->update([
            'learnVideoSectionCount' => $allSectionCount
        ]);
    }
}
